////////////////////////////////////////////////////////////////////////////////
//  COPYRIGHT (C) 2010 TELECOM DIGITAL MEDIA ("TDMEDIA").
//  ALL RIGHTS RESERVED.
//
//  THIS IS CONFIDENTIAL AND PROPRIETARY INTELLECTUAL PROPERTY OWNED BY AND
//  CREATED ON BEHALF OF TDMEDIA. ANY FORM OF DISTRIBUTION, COPY,
//  MODIFICATION WITHOUT THE WRITTEN CONSENT FROM TDMEDIA IS STRICTLY
//  PROHIBITED.
////////////////////////////////////////////////////////////////////////////////
package cc.telecomdigital.tdstock.activity.groups.more;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;
import cc.telecomdigital.tdstock.CommonDefn;
import cc.telecomdigital.tdstock.ITDLApplication;
import cc.telecomdigital.tdstock.R;
import cc.telecomdigital.tdstock.StringUtils;
import cc.telecomdigital.tdstock.Framework.ActivityGroup_Framework;
import cc.telecomdigital.tdstock.Framework.UniqueItem.TDStockLog;
import cc.telecomdigital.tdstock.Http.H_CODE;
import cc.telecomdigital.tdstock.Http.HttpSuperClassStockBaseActivity;
import cc.telecomdigital.tdstock.Http.Services.HttpAppInfo;
import cc.telecomdigital.tdstock.Http.Services.H_HttpServiceRequest.H_UserClient_HttpRequest_Result;
import cc.telecomdigital.tdstock.Tools.ActivityGroupController;
import cc.telecomdigital.tdstock.activity.groups.MoreGroup;
import cc.telecomdigital.tdstock.activity.groups.StockGroup;
import cc.telecomdigital.tdstock.activity.groups.stock.BMPCurrentStockInfoActivity;
import cc.telecomdigital.tdstock.activity.groups.stock.CurrentStockInfoActivity;
import cc.telecomdigital.tdstock.adapter.AHListItemAdapter;
import cc.telecomdigital.tdstock.adapter.AHViewHolder;

/**
 * @author haibo for list item named "AH Comparison"
 */
public class AHCompActivity extends HttpSuperClassStockBaseActivity
{
	protected static final String	TAG			= AHCompActivity.class.getSimpleName();

	private static final long		START_ID	= 90000l;

	private TextView				_hk2rmb_rate;
	private ListView				_listView;
	private Button					_toolBtnReturn;

	private AHListItemAdapter		_ahAdapter;
	private List<AHViewHolder>		_ahList;
	private String					_rate;
	private SharedPreferences		_prefsStockCode;

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.more_ah_comp);

		_toolBtnReturn = (Button) findViewById(R.id.more_returnbtn);
		_toolBtnReturn.setFocusable(true);
		_toolBtnReturn.setClickable(true);
		_toolBtnReturn.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				TDStockLog.d(TAG, "close current activity and forwarding to the previous");
				
				SwitchBackActivity();
			}
		});

		_messageHandler = null; // this activity no need it.

		_hk2rmb_rate = (TextView) findViewById(R.id.hk2rmb_rate);
		_listView = (ListView) findViewById(R.id.more_ah_list);

		_ahList = new ArrayList<AHViewHolder>();
		_ahAdapter = new AHListItemAdapter(LayoutInflater.from(this), _ahList);

		_listView.setOnItemClickListener(new OnItemClickListener()
		{
			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id)
			{
				// the first line is the title
				if (position > 0 && _ahList.size() >= position + 1)
				{
					String stockCode = String.valueOf(Integer
							.parseInt(_ahList.get(position).itemName
									.getText().toString()
									.split("[  ]")[0].trim()));
					_prefsStockCode = getSharedPreferences(
							CommonDefn.SYSTEM_CURRENT_SETTING,
							Context.MODE_PRIVATE);
					_prefsStockCode.edit().putString(
							CommonDefn.PREFERENCE_STOCK_CODE, stockCode)
							.commit();

					// BMP Mode by ouyangzhihui 2011-10-13
					Class<? extends ActivityGroup_Framework> _cls = CurrentStockInfoActivity.class;
					if (((ITDLApplication) getApplicationContext()).getIsBMPMode())
					{
						_cls = BMPCurrentStockInfoActivity.class;
					}

					Intent intent = new Intent(getApplicationContext(), _cls);
					intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NO_USER_ACTION);

					SwitchTabHostActivity(0, StockGroup.GetActivityGroupHostControl(), _cls, intent);
				}
			}
		});

		_listView.setAdapter(_ahAdapter);
	}

	private void updateAdapter()
	{
		_ahAdapter.notifyDataSetChanged();
		if (_rate != null)
		{
			_hk2rmb_rate.setText(String.format("HK$1=$%sRM", _rate));
			_hk2rmb_rate.invalidate();
		}
		_listView.setClickable(true);
		_listView.setEnabled(true);
	}

	@Override
	protected void onResume()
	{
		super.onResume();
		_listView.invalidateViews();
	}

	private void updateData(List<String[]> arrayList)
	{
		_rate = arrayList.get(0)[1];
		_ahList.clear();
		arrayList.remove(0);
		{
			AHViewHolder holder = new AHViewHolder();
			holder.id = START_ID - 1;
			holder.itemName = new TextView(AHCompActivity.this);
			String itemName = StringUtils.fillStringToStaticLength("", " ", 12, true)
					+ getResources().getString(R.string.more_ah_column_0);
			holder.itemName.setText(itemName);
			holder.column1 = new TextView(
					AHCompActivity.this);
			holder.column1
					.setText(R.string.more_ah_column_1);
			holder.column2 = new TextView(
					AHCompActivity.this);
			holder.column2
					.setText(R.string.more_ah_column_2);
			holder.column3 = new TextView(
					AHCompActivity.this);
			holder.column3
					.setText(R.string.more_ah_column_3);
			_ahList.add(holder);
		}
		int index = 0;
		long id = START_ID + _ahList.size();
		for (String[] array : arrayList)
		{
			if (array[0] == null || array[0].length() < 1)
			{
				continue;
			}
			AHViewHolder holder = new AHViewHolder();
			holder.id = id + index;
			holder.itemName = new TextView(AHCompActivity.this);
			holder.itemName.setText(String.format("%05d %s", Integer.parseInt(array[0]), array[1]));
			holder.column1 = new TextView(AHCompActivity.this);
			holder.column1.setText(array[2] == null ? CommonDefn.DEFAULT_FIELD_VALUE : array[2]);
			holder.column2 = new TextView(AHCompActivity.this);
			holder.column2.setText(array[3] == null ? CommonDefn.DEFAULT_FIELD_VALUE : array[3]);
			holder.column3 = new TextView(AHCompActivity.this);
			holder.column3.setText(array[4] == null ? CommonDefn.DEFAULT_FIELD_VALUE : array[4]);
			_ahList.add(holder);
			index++;
		}
	}

	@Override
	public ActivityGroupController GetActivityGroupController()
	{
		return MoreGroup.GetActivityGroupHostControl();
	}

	@Override
	protected H_CODE GetDataFromHttpServer()
	{
		return HttpAppInfo.SendAHComparisonRequest(actContext, this);
	}

	@Override
	public void H_UserClient_HttpRequest_Response_Sub(final H_UserClient_HttpRequest_Result iResult)
	{
		if (iResult.MStatus.equals("0") == false)
		{
			// Prompt Error
			ShowMessageDialog(iResult.MError + "(" + iResult.MStatus + ")");
			return;
		}

		Runnable runnable = new Runnable()
		{
			@Override
			public void run()
			{
				updateData(iResult.contentStores);

				updateAdapter();
			}
		};
		_messageHandler.post(runnable);
	}

}
